package com.example.librarymanagement.service.impl;

import com.example.librarymanagement.mapper.BorrowRecordMapper;
import com.example.librarymanagement.model.BorrowRecord;
import com.example.librarymanagement.service.BookService;
import com.example.librarymanagement.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BorrowRecordServiceImpl implements BorrowRecordService {

    private final BorrowRecordMapper borrowRecordMapper;
    private final BookService bookService; // 假设已有BookService来更新图书状态

    @Autowired
    public BorrowRecordServiceImpl(BorrowRecordMapper borrowRecordMapper, BookService bookService) {
        this.borrowRecordMapper = borrowRecordMapper;
        this.bookService = bookService;
    }

    @Override
    public BorrowRecord createBorrowRecord(Integer bookId, Integer userId) {
        BorrowRecord record = new BorrowRecord();
        record.setBookId(bookId);
        record.setUserId(userId);
        // 使用java.sql.Date来截取日期部分
        record.setBorrowDate(new java.sql.Date(System.currentTimeMillis()));
        borrowRecordMapper.insertBorrowRecord(record);
        return record;
    }


    @Override
    public void returnBook(Integer recordId, Integer userId) {
        // 截取当前日期，忽略时间部分
        java.sql.Date returnDate = new java.sql.Date(System.currentTimeMillis());
        borrowRecordMapper.updateReturnDate(recordId, returnDate);
    }
    @Override
    public List<BorrowRecord> listAllBorrowRecords() {
        return borrowRecordMapper.selectAllWithDetails();
    }
    @Override
    public List<BorrowRecord> listBorrowRecordsByUser(Integer userId) {
        return borrowRecordMapper.selectBByUserId(userId);
    }


}